﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
using System.Text;

/// <summary>
///S_District 的摘要说明
/// </summary>
public partial class S_District
{

    public static S_District GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.S_District.FirstOrDefault(row => row.DistrictID == id);
        }
    }
    public static string GetDistrictSByIDs(object ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            string Sql = String.Format("select DistrictName+',' from dbo.S_District where CONVERT(varchar,DistrictID) in ({0}) For XML PATH('') ", ID.ToString());
            return context.ExecuteQuery<string>(@Sql).FirstOrDefault();
        }
    }

    public static List<S_District> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.S_District.ToList();
        }
    }

    public static string GetPName_CName_RNameByPID_CID_RID(object PID, object CID, object RID)
    {
        StringBuilder Return = new StringBuilder();
        S_Province objP = S_Province.GetByID(DongDong.Common.Misc.ConvertToInt(PID));
        if (objP != null)
            Return.Append(objP.ProvinceName);
        S_City objC = S_City.GetByID(DongDong.Common.Misc.ConvertToInt(CID));
        if (objC != null)
            Return.Append(objC.CityName);
        S_District objR = S_District.GetByID(DongDong.Common.Misc.ConvertToInt(RID));
        if (objR != null)
            Return.Append(objR.DistrictName);
        return Return.ToString();
    }

    public static string GetPName_CNameByPID_CID(object PID, object CID)
    {
        StringBuilder Return = new StringBuilder();
        S_Province objP = S_Province.GetByID(DongDong.Common.Misc.ConvertToInt(PID));
        if (objP != null)
            Return.Append(objP.ProvinceName);
        S_City objC = S_City.GetByID(DongDong.Common.Misc.ConvertToInt(CID));
        if (objC != null)
            Return.Append(objC.CityName);
        return Return.ToString();
    }

    public static string GetQuName(string Areas)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.ExecuteQuery<string>(@"SELECT  DistrictName+',' FROM dbo.S_District WHERE DistrictID in ({0}) FOR XML PATH('')", Areas).FirstOrDefault();
        }
    }

    public static List<S_District> GetAllByCondition(int CityID, string key)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from c in context.S_District
                        select c;
            if (CityID != 0)
                query = query.Where(row => row.CityID == CityID);
            if (!string.IsNullOrEmpty(key))
                query = query.Where(row => row.DistrictName.Contains(key));
            return query.ToList();
        }
    }


    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.S_District.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<S_District>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.S_District.FirstOrDefault(row => row.DistrictID == id);
            if (objItem == null)
                return;
            context.S_District.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
}